package org.scala.javafx.diagnostics

import org.scala.javafx.rich.Enrich._
import javafx.scene.control.TreeItem
import javafx.scene.Node
import javafx.scene.Parent
import scala.collection.JavaConverters._
import javafx.scene.Scene
import javafx.collections.ObservableList

case class SpyTreeItem(val spyInfo: SpyInfo) extends TreeItem[SpyInfo](spyInfo, null) {
  def isRoot = false
  
  override def isLeaf(): Boolean = spyInfo.children.size == 0
  
  lazy val populate: ObservableList[TreeItem[SpyInfo]] = {
    spyInfo.children.foreach { infoChild => this.getChildren().add(new SpyTreeItem(infoChild)) }
    
    this.getChildren()
  }
  
  if (isRoot) {
    this.populate.asScala.foreach { _.asInstanceOf[SpyTreeItem].populate }
  }

  this.expandedProperty().addFirstChangeListener { (_, _, newExpanded) =>
    this.populate.asScala.foreach { _.asInstanceOf[SpyTreeItem].populate }
  }
}
